package com.cuijq.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuijq.eduschool.commontuils.R;
import com.cuijq.eduservice.entity.EduTeacher;
import com.cuijq.eduservice.query.TeacherQuery;
import com.cuijq.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-24
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

  @Autowired
  private EduTeacherService teacherService;

  @ApiOperation(value = "所有讲师列表")
  @GetMapping("findAll")
  public R findAllTeacher() {
    List list = teacherService.list(null);
    return R.ok().data("items", list);
  }

  @ApiOperation(value = "根据Id删除讲师")
  @DeleteMapping("{id}")
  public R removeById(
      @ApiParam(name = "id", value = "讲师ID", required = true)
      @PathVariable String id) {
    boolean flag = teacherService.removeById(id);
    if (flag) {
      return R.ok();
    } else {
      return R.error();
    }
  }

  @ApiOperation(value = "分页讲师列表")
  @GetMapping("pageTeacher/{page}/{limit}")
  public R pageListTeacher(
      @ApiParam(name = "page", value = "当前页码", required = true)
      @PathVariable long page,
      @ApiParam(name = "limit", value = "每页记录数", required = true)
      @PathVariable long limit
  ) {
    Page<EduTeacher> pageTeacher = new Page<>(page, limit);
    teacherService.page(pageTeacher, null);
    List<EduTeacher> records = pageTeacher.getRecords();
    long total = pageTeacher.getTotal();
    return R.ok().data("total", total).data("rows", records);//
  }

  @ApiOperation(value = "分页讲师列表")
  @PostMapping("pageTeacherCondition/{current}/{limit}")
  public R pageListTeacherCondition(
      @ApiParam(name = "page", value = "当前页码", required = true)
      @PathVariable long current,
      @ApiParam(name = "limit", value = "每页记录数", required = true)
      @PathVariable long limit,
      @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
      @RequestBody(required = false) TeacherQuery teacherQuery
  ) {
    Page<EduTeacher> pageTeacher = new Page<>(current, limit);
    QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
    String name = teacherQuery.getName();
    Integer level = teacherQuery.getLevel();
    String begin = teacherQuery.getBegin();
    String end = teacherQuery.getEnd();
    if (!StringUtils.isEmpty(name)) {
      //构建条件
      wrapper.like("name", name);
    }
    if (!StringUtils.isEmpty(level)) {
      wrapper.eq("level", level);
    }
    if (!StringUtils.isEmpty(begin)) {
      wrapper.ge("gmt_create", begin);
    }
    if (!StringUtils.isEmpty(end)) {
      wrapper.le("gmt_create", end);
    }
    wrapper.orderByDesc("gmt_create");
    teacherService.page(pageTeacher, wrapper);
    List<EduTeacher> records = pageTeacher.getRecords();
    long total = pageTeacher.getTotal();
    return R.ok().data("total", total).data("rows", records);//
  }

  //添加讲师接口的方法
  @PostMapping("addTeacher")
  public R addTeacher(@RequestBody EduTeacher eduTeacher) {
    boolean save = teacherService.save(eduTeacher);
    if (save) {
      return R.ok();
    } else {
      return R.error();
    }
  }

  //根据讲师id进行查询
  @GetMapping("getTeacher/{id}")
  public R getTeacher(@PathVariable String id) {
    EduTeacher eduTeacher = teacherService.getById(id);
    return R.ok().data("teacher", eduTeacher);
  }

  //讲师修改功能
  @PostMapping("updateTeacher")
  public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
    boolean flag = teacherService.updateById(eduTeacher);
    if (flag) {
      return R.ok();
    } else {
      return R.error();
    }
  }

  //讲师修改功能
  @ApiOperation(value = "逻辑删除讲师")
  @DeleteMapping("delete/{id}")
  public R removeById(@PathVariable Long id) {
    boolean flag = teacherService.removeById(id);
    if (flag) {
      return R.ok();
    } else {
      return R.error();
    }
  }

  //2 逻辑删除讲师的方法
//  @ApiOperation(value = "逻辑删除讲师")
//  @DeleteMapping("{id}")
//  public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
//  @PathVariable String id) {
//    boolean flag = teacherService.removeById(id);
//    if (flag) {
//      return R.ok();
//    } else {
//      return R.error();
//    }
//  }

}

